/**
 * Created by developer on 17-2-22.
 */
var $id;
var $status;
var $rightSlider = $(".aside-slider-content");
var $arr = ["size", "download_size"];
var $cover = $(".masking");

$(function () {
    var editPropertyIndex = null;
    //新增新商品属性
    $(".add-product-template").on("click", function () {
        $(".container-product-template").children(".panel_header").html("新增商品基础属性模板<span class='glyphicon glyphicon-remove cancel'></span>");
        modifyDisplayBlock($(".container-product-template"));
        //关闭弹出框
        $(".container-product-template").find(".cancel").on("click", function () {
            modifyDisplayNone($(".container-product-template"));
        });
        //"保存并添加下一个"按钮显示
        $("#save_add_next").show();
    });
    //列表点击事件，出现侧栏
    $(".list-item").on("click", function () {
        // console.log($(this).attr("id"));
        var $currentId = $(this).attr("id");
        expandOrCloseSidebar(getStatus($currentId), $currentId);
    });
    //关闭侧栏
    $rightSlider.find(".cancel").on("click", function () {
        $rightSlider.animate({right: "-400px"});
        $status = "close";
    });
    //编辑商品属性
    $("#button_edit_product_attribute").on("click", function () {
        $(".container-product-template").children(".panel_header").html("修改商品基础属性模板<span class='glyphicon glyphicon-remove cancel'></span>");
        modifyDisplayBlock($(".container-product-template"));
        //关闭弹出框
        $(".container-product-template").find(".cancel").on("click", function () {
            modifyDisplayNone($(".container-product-template"));
        });
        //"保存并添加下一个"按钮隐藏
        $("#save_add_next").hide();
    });
    //删除商品属性
    $("#button_delete_product_attribute").on("click", function () {
       modifyDisplayBlock($(".container-delete-product-template"));
    });
    //关闭删除框
    $(".container-delete-product-template").find(".cancel").on("click", function () {
        modifyDisplayNone($(".container-delete-product-template"));
    });
    //添加属性
    $("#button_add_product_attribute").on("click",function(){
        modifyDisplayBlock($(".modal-large"))
    });
    //关闭添加商品属性
    $(".modal-large .cancel").on("click",function(){
        modifyDisplayNone($(".modal-large"))
    });
    //点击修改编辑商品属性赋值事件
    $(".modal-large .attribute_list_group").on("click",".glyphicon-edit",function () {
        $(".sure-attribute").show();
        $(".cancel-sure").show();
        $(".add-attribute").hide();
        $(".cancel-add").hide();
        var propertyName = $(this).siblings(".property_name").text();
        var propertyValue = $(this).siblings(".property_value").text();
        var currentLiElement = $(this).parent();
        $("#attribute_name").val(propertyName);
        $("#attribute_value").val(propertyValue);
        editPropertyIndex = currentLiElement.index();
        console.log(editPropertyIndex);
    });
    //点击添加商品属性
    $(".modal-large .add-attribute").on("click",function () {
        var propertyName = $("#attribute_name").val();
        var propertyValue = $("#attribute_value").val();
        $("#attribute_name").val("");
        $("#attribute_value").val("");
        var child = '<li class="attribute_list_item row">'
                + '<span class="col-sm-2 property_name">' + propertyName + '</span>'
                + '<span class="col-sm-8 property_value">' + propertyValue + '</span>'
                + '<span class="col-sm-1 glyphicon glyphicon-edit"></span>'
                + '<span class="col-sm-1 glyphicon glyphicon-remove"></span></li>';
        $(".attribute_list_group").append(child);
    });
    //点击确认更新商品属性
    $(".modal-large .sure-attribute").on("click",function () {
        var propertyName = $("#attribute_name").val();
        var propertyValue = $("#attribute_value").val();
        var currentLiElement = $(".modal-large .attribute_list_item")[editPropertyIndex];
        $(currentLiElement).children(".property_name").text(propertyName);
        $(currentLiElement).children(".property_value").text(propertyValue);
        $("#attribute_name").val("");
        $("#attribute_value").val("");
        editPropertyIndex = null;
        $(".sure-attribute").hide();
        $(".cancel-sure").hide();
        $(".add-attribute").show();
        $(".cancel-add").show();
    });
    //点击取消修改商品属性
    $(".modal-large .cancel-sure").on("click",function () {
        $("#attribute_name").val("");
        $("#attribute_value").val("");
        editPropertyIndex = null;
        $(".sure-attribute").hide();
        $(".cancel-sure").hide();
        $(".add-attribute").show();
        $(".cancel-add").show();
    });
    //点击取消添加商品属性
    $(".modal-large .cancel-add").on("click",function () {
        $("#attribute_name").val("");
        $("#attribute_value").val("");
    });
    //点击删除商品属性
    $(".modal-large .attribute_list_group").on("click",".glyphicon-remove", function () {
        $(this).parent().remove();
    });
});
//获取页面侧栏的打开或关闭的状态
function getStatus(currentId) {
    if ($id == currentId) {
        $status = "close";
        $id = "";
    } else {
        $status = "open";
        $id = currentId;
    }
    console.log($id);
    return $status;
}
//通过id对比打开或关闭页面侧栏
function expandOrCloseSidebar($status, currentId) {
    if ($status == "close") {
        $rightSlider.animate({right: "-400px"});
    } else if($status == "open") {
        for (var i = 0; i < $arr.length; i++) {
            if ($arr[i] == currentId) {
                $rightSlider.animate({right: "0px"});
            } else {
                console.log("没有对应的商品属性");
            }
        }
    }
}
//修改css样式display为block
function modifyDisplayBlock(obj) {
    obj.css("display", "block");
    $cover.css("display", "block");
}
//修改css样式display为none
function modifyDisplayNone(obj) {
    obj.css("display", "none");
    $cover.css("display", "none");
}